package org.one.stone.soup.grfxML.plugin.control;

import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;

public class Rotator extends SimplePlugin {


	public static final int ARG_ROTATION = 0;

	public static final int ARG_ROTATECLOCKWISE = 1;

	public static final int ARG_ROTATEANTICLOCKWISE = 2;

	org.one.stone.soup.grfxML.DataSize rotation = new org.one.stone.soup.grfxML.DataSize();

	org.one.stone.soup.grfxML.DataState rotateClockwise = new org.one.stone.soup.grfxML.DataState();

	org.one.stone.soup.grfxML.DataState rotateAnticlockwise = new org.one.stone.soup.grfxML.DataState();

	int dr = 0;
	int cap = 10;

public Rotator (GrfxMLEngine engine){
	super(engine);
}

public void initialize() {}

public void process()
{
 try{
	if(rotateAnticlockwise.getValue()==true)
	{
		if(dr<0)
			dr=0;
		if(dr<cap)
			dr++;
		rotation.setValue( rotation.getValue()+dr,this );
	}
	if(rotateClockwise.getValue()==true)
	{
		if(dr>0)
			dr=0;
		if(dr>-cap)
			dr--;
		rotation.setValue( rotation.getValue()+dr,this );
	}
 }catch(LockException le){}
}

public void register(DataLinker store)
{

	rotation = grfxMLCaster.cast(rotation,getArg(rotation,ARG_ROTATION,store));

	rotateClockwise = grfxMLCaster.cast(rotateClockwise,getArg(rotateClockwise,ARG_ROTATECLOCKWISE,store));

	rotateAnticlockwise = grfxMLCaster.cast(rotateAnticlockwise,getArg(rotateAnticlockwise,ARG_ROTATEANTICLOCKWISE,store));

}

public void replace(Field oldObj, Field newObj)
{

	rotation = grfxMLCaster.replace(rotation,oldObj,newObj);

	rotateClockwise = grfxMLCaster.replace(rotateClockwise,oldObj,newObj);

	rotateAnticlockwise = grfxMLCaster.replace(rotateAnticlockwise,oldObj,newObj);

}

public void stop() {}
}
